# -*- coding: utf-8 -*-
"""
@Time ： 2022/12/29 16:50
@Auth ： jiraWu
@File ：_requests.py
@IDE ：PyCharm
@Motto:人生苦短，我学Python

"""

"""
requests模块的封装
"""
import requests
from _xpath import HtmlTree


class Requests:
    pass


class GetRequests(Requests):
    def __init__(self, url: str, params=None, headers=None):
        if headers is None:
            headers = {}
        if params is None:
            params = {}
        self.url = url
        self.params = params
        self.headers = headers

    def send_req(self) -> dict:
        """
        发起请求
        :return: 返回响应头
        """
        resp = requests.get(url=self.url, params=self.params, headers=self.headers)
        return {'url': resp.url, 'text': resp.text, 'cont': resp.content, 'status': resp.status_code,
                'cookies': resp.cookies, 'encoding': resp.encoding}

    def __str__(self):
        return f"请求地址:{self.url},请求方法:【GET】,请求头:{self.headers},请求参数:{self.params}"


class PostRequests(Requests):
    pass


if __name__ == '__main__':
    url = "https://www.baidu.com"
    resp = GetRequests(url=url).send_req()
    html = (resp['cont'].decode('utf-8'))
    tree = HtmlTree(html).analysis("//input[@id='su']")
    print(tree)
